index.js ➔ create   B
last analyzed

Complexity

Conditions 1
Paths 2

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1.0015

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
c 4
b 0
f 1
nc 2
nop 2
dl 0
loc 88
ccs 23
cts 26
cp 0.8846
crap 1.0015
rs 8.6012

4 Functions

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ... ➔ hasProperty 0 3 1
B index.js ➔ ... ➔ check 0 24 1
A index.js ➔ ... ➔ getLastClick 0 8 2
A index.js ➔ ... ➔ save 0 11 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"use strict";
2
3 1
module.exports = create;
4
5 1
const cookie = require('cookie');
6 1
const queryString = require('query-string');
7 1
const merge = require('merge-objects');
8
9
function create(document, settings) {
10 2
    settings = merge(settings || {}, {
11
        sources: [
12
            {referrer: 'google'},
13
            {referrer: 'yahoo'},
14
            {referrer: 'bing'},
15
            {queryParameter: 'gclid', 'value': 'adwords'},
16
            {queryParameter: 'msclkid', 'value': 'bingads'},
17
            {queryParameter: 'utm_source'}
18
        ],
19
        cookie: {
20
            name: 'last_click',
21
            expires: 30 * 24 * 60 * 60 // 30 days in seconds
22
        }
23
    });
24
25 2
    const location = document.location;
26
27 2
    const obj = {
28
        getLastClick: getLastClick,
29
        check: check
30
    };
31
32 2
    obj.check();
33
34 2
    return obj;
35
36
    /**
37
     * @param {String} value
38
     */
39
    function save(value) {
40 4
        const date = new Date();
41 4
        date.setTime(date.getTime() + (settings.cookie.expires * 1000));
42
43 4
        document.cookie = cookie.serialize(settings.cookie.name, value, {
0 ignored issues
show
Bug introduced by
The variable document seems to be never initialized.
Loading history...
44
            httpOnly: true,
45
            maxAge: settings.cookie.expires,
46
            expires: date,
47
            path: '/'
48
        });
49
    }
50
51
    /**
52
     * @return {String|*}
53
     */
54
    function getLastClick() {
55 2
        const cookies = cookie.parse(document.cookie);
0 ignored issues
show
Bug introduced by
The variable document seems to be never initialized.
Loading history...
56 2
        if(hasProperty(cookies, settings.cookie.name)) {
57 2
            return cookies[settings.cookie.name];
58
        }
59
60
        return null;
61
    }
62
63
    function check() {
64 4
        const queryParams = queryString.parse(location.search);
65 4
        settings.sources.forEach(function (source) {
66 24
            let val = '';
67
68 24
            if(hasProperty(source, 'queryParameter') && hasProperty(queryParams, source.queryParameter)) {
69 2
                val = queryParams[source.queryParameter];
70 22
            } else if(hasProperty(source, 'referrer') && document.referrer && document.referrer.indexOf(source.referrer) >= 0) {
0 ignored issues
show
Bug introduced by
The variable document seems to be never initialized.
Loading history...
71 2
                val = document.referrer;
72
            }
73
74 24
            if(val) {
75 4
                if(hasProperty(source, 'value')) {
76 2
                    if(typeof(source.value) === 'function') {
77
                        val = source.value.apply(val);
78
                    } else {
79
                        val = source.value;
80
                    }
81
                }
82
83 4
                save(val);
84
            }
85
        });
86
    }
87
88
    /**
89
     * @param {Object} obj
90
     * @param {String} property
91
     * @return {boolean}
92
     */
93
    function hasProperty(obj, property) {
94 64
        return Object.keys(obj).indexOf(property) !== -1
95
    }
96
}